dnl Process this file with autoconf to produce a configure script.
AC_INIT(openCryptoki, 2.2.6, opencryptoki-tech@lists.sourceforge.net,)

dnl Backup CFLAGS so that once the auto tools set it to "-g -O2",
dnl (which we want to avoid), we can restore them to whatever the
dnl user might have wanted below.
cmdline_CFLAGS="$CFLAGS"

# Compute $target
AC_CANONICAL_TARGET

AM_INIT_AUTOMAKE([foreign 1.6])

dnl Checks for programs.

dnl Checks for libraries.

dnl Checks for header files.
AC_DISABLE_STATIC
AC_PROG_LIBTOOL

AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h strings.h sys/file.h syslog.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_UID_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_STRUCT_TM
dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_MEMCMP
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([getcwd])

# Used in the init scripts
AC_SUBST(ID, /usr/bin/id)
AC_SUBST(USERMOD, /usr/sbin/usermod)
AC_SUBST(GROUPADD, /usr/sbin/groupadd)

dnl i.e., /usr/local or /usr
if test "x$prefix" = xNONE; then
  prefix=$ac_default_prefix
fi
if test "x$exec_prefix" = xNONE; then
  exec_prefix='${prefix}'
fi

CFLAGS="$cmdline_CFLAGS"

s390=0

# Arch specific stuff
case $target in
	*64*)
		lib="lib64"
		AM_CONDITIONAL(S390, false) ;;
	*s390x*)
		s390=1
		lib="lib64"
		AM_CONDITIONAL(S390, true) ;;
	*s390*)
		s390=1
		lib="lib"
		AM_CONDITIONAL(S390, true) ;;
        *)
		lib="lib"
		AM_CONDITIONAL(S390, false) ;;
esac

if test "x$prefix" == x/usr; then
        AC_SUBST(CONFIG_PATH, /var/lib/opencryptoki)
	AC_SUBST(CONFIG_PATH_ETC, /etc)
        AC_SUBST(SBIN_PATH, /usr/sbin)
        AC_SUBST(DB_PATH, /var/lib/opencryptoki)
else
        AC_SUBST(CONFIG_PATH, $prefix/var/lib/opencryptoki)
	AC_SUBST(CONFIG_PATH_ETC, $prefix/etc)
        AC_SUBST(SBIN_PATH, $prefix/sbin)
        AC_SUBST(DB_PATH, $prefix/var/lib/opencryptoki)
fi

AC_SUBST(LIB_PATH, $prefix/$lib/opencryptoki)
AC_SUBST(STDLL_PATH, $LIB_PATH/stdll)
AC_SUBST(CONFIG_FILE, pk_config_data)
AC_SUBST(METHOD_PATH, $prefix/sbin)
AC_SUBST(HEADER_PATH, $prefix/include)

# Debugging support
AC_ARG_ENABLE(debug, [  --enable-debug          turn on all openCryptoki debugging flags],
                     [ enable_debug="yes"],)

if test "x$enable_debug" = xyes; then
	CFLAGS="$CFLAGS -g -O0 -DDEBUG -DDEBUGON"
	AC_MSG_RESULT([*** Enabling debugging at user request ***])
else
	CFLAGS="$CFLAGS -O2"
fi

# Support for OpenSSL path specification
AC_ARG_WITH(openssl,
        [  --with-openssl[[=DIR]]    build with OpenSSL support [[/usr/local/ssl]]],
        [openssl_prefix=$withval], 
        [openssl_prefix=]
)
if test "x$openssl_prefix" != x; then
	AC_MSG_RESULT([*** Using OpenSSL directory $openssl_prefix ***])
	LDFLAGS="-L$openssl_prefix/lib $LDFLAGS"
	CFLAGS="-I$openssl_prefix/include $CFLAGS"
fi

# Check if building daemon
AC_ARG_ENABLE(daemon, [  --disable-daemon        don't build pkcsslotd [default=enabled]],
              [AM_CONDITIONAL(DAEMON, false)],
              [AM_CONDITIONAL(DAEMON, true)])

# Check if building library
AC_ARG_ENABLE(library, [  --disable-library       don't build libopencryptoki [default=enabled]],
              [AM_CONDITIONAL(LIBRARY, false)],
              [AM_CONDITIONAL(LIBRARY, true)])

#
# s390 tokens
#

# The ica token is enabled by default on s390
if [[ $s390 -eq 1 ]]; then

AM_CONDITIONAL(ICA, false)
AC_ARG_ENABLE(ica390tok,
              [  --disable-ica390tok     s390 only: don't build the ICA token [default=enabled]],
              [AM_CONDITIONAL(DEFAULT_DLL, false)],
              [enable_ica390tok="yes"])

if test "x$enable_ica390tok" = xyes; then
        AM_CONDITIONAL(DEFAULT_DLL, true)
else
        AC_MSG_RESULT([*** Disabling the default ICA token at user request ***])
fi

else
#
# non s390 tokens
#

# The software token is enabled by default on non-s390 platforms
AC_ARG_ENABLE(swtok,
              [  --disable-swtok         don't build the software token [default=enabled (except s390)]],
              [AM_CONDITIONAL(DEFAULT_DLL, false)],
              [enable_swtok="yes"])

if test "x$enable_swtok" = xyes; then
        AC_CHECK_LIB(crypto, AES_encrypt,
             [AM_CONDITIONAL(DEFAULT_DLL, true)],
             [AM_CONDITIONAL(DEFAULT_DLL, false)])
else
        AC_MSG_RESULT([*** Disabling the default software token at user request ***])
fi

# Support for the IBM Crypto Accelerator (PCICA) token
AC_ARG_ENABLE(icatok,
              [  --enable-icatok         build the IBM Crypto Accelerator (PCICA) token [default=disabled]],
              [enable_icatok="yes"],
              [enable_icatok="no"])

if test "x$enable_icatok" = xyes; then
	AC_CHECK_HEADER(ica_api.h,
			AC_CHECK_LIB(ica, icaOpenAdapter,
				[AM_CONDITIONAL(ICA, true)],
				[AM_CONDITIONAL(ICA, false)]),
			[AM_CONDITIONAL(ICA, false)],,)
else 
	AM_CONDITIONAL(ICA, false)
fi

fi

# TPM support for the TPM token
AC_ARG_ENABLE(tpmtok,
              [  --enable-tpmtok         build the TPM token [default=disabled]],
              [enable_tpmtok="yes"],
              [enable_tpmtok="no"])

if test "x$enable_tpmtok" = xyes; then
	AC_CHECK_HEADER(tss/platform.h,
	AC_CHECK_LIB(crypto, AES_encrypt,
			AC_CHECK_LIB(tspi, Tspi_Context_Create,
				     [AM_CONDITIONAL(TPM, true)],
                                     [AM_CONDITIONAL(TPM, false)], 
                                     -lcrypto),
			[AM_CONDITIONAL(TPM, false)]),
		[AM_CONDITIONAL(TPM, false)])
else
	AM_CONDITIONAL(TPM, false)
fi

# Support for the IBM 4758 (PCICC) token
AC_ARG_ENABLE(icctok,
              [  --enable-icctok         build the IBM 4758 (PCICC) token [default=disabled]],
              [enable_icctok="yes"],
              [enable_icctok="no"])

if test "x$enable_icctok" = xyes; then
	AC_CHECK_HEADER(scc_host.h,
			AC_CHECK_LIB(scc, sccOpenAdapter,
				[AM_CONDITIONAL(ICC, true)],
				[AM_CONDITIONAL(ICC, false)]),
			[AM_CONDITIONAL(ICC, false)],,)
else
	AM_CONDITIONAL(ICC, false)
fi

# Support for the secure-key (CCA api) token
AC_ARG_ENABLE(ccatok,
              [  --enable-ccatok         build the IBM CCA Secure-Key token [default=disabled]],
              [enable_ccatok="yes"],
              [enable_ccatok="no"])

if test "x$enable_ccatok" = xyes; then
	AM_CONDITIONAL(CCA, true)
else
	AM_CONDITIONAL(CCA, false)
fi

# Support for the AEP Crypto Accelerator
AC_ARG_ENABLE(aeptok,
              [  --enable-aeptok         build the AEP Crypto Accelerator token [default=disabled]],
              [enable_aeptok="no"],
              [enable_aeptok="yes"])

if test "x$enable_aeptok" = xyes; then
        AC_CHECK_LIB(aep, AEP_Initialize,
             [AM_CONDITIONAL(AEP, true)],
             [AM_CONDITIONAL(AEP, false)])
else
        AM_CONDITIONAL(AEP, false)
fi

# Support for the Broadcom Crypto Accelerator
AC_ARG_ENABLE(bcomtok,
              [  --enable-bcomtok        build the Broadcom Crypto Accelerator token [default=disabled]],
              [enable_bcomtok="no"],
              [enable_bcomtok="yes"])

if test "x$enable_bcomtok" = xyes; then
	AC_CHECK_HEADER(ubsec.h,
			AC_CHECK_LIB(ubsec, ubsec_open,
				[AM_CONDITIONAL(BCOM, true)],
				[AM_CONDITIONAL(BCOM, false)]),
			[AM_CONDITIONAL(BCOM, false)],,)
else
	AM_CONDITIONAL(BCOM, false)
fi

# Support for the Corrent Crypto Accelerator
AC_ARG_ENABLE(crtok,
              [  --enable-crtok          build the Corrent Crypto Accelerator token [default=disabled]],
              [enable_crtok="no"],
              [enable_crtok="yes"])

if test "x$enable_crtok" = xyes; then
	AC_CHECK_HEADER(typhoon.h,
			AC_CHECK_LIB(socketarmor, CR_init_lib,
				[AM_CONDITIONAL(CR, true)],
				[AM_CONDITIONAL(CR, false)], -ldl),
			[AM_CONDITIONAL(CR, false)],,)
else
        AM_CONDITIONAL(CR, false)
fi

# Check if building tests
AC_ARG_ENABLE(testcases,
              [  --enable-testcases      build the test cases [default=disabled]],
              [AM_CONDITIONAL(TESTS, true)],
              [AM_CONDITIONAL(TESTS, false)])


CFLAGS="$CFLAGS -DPKCS64			\
	-DCONFIG_PATH=\\\"$CONFIG_PATH\\\"	\
	-DSBIN_PATH=\\\"$SBIN_PATH\\\"		\
	-DLIB_PATH=\\\"$LIB_PATH\\\"		\
	-D_XOPEN_SOURCE=500"

# At this point, CFLAGS is set to something sensible
AC_PROG_CC

AC_OUTPUT([Makefile usr/Makefile \
          usr/include/Makefile \
          usr/include/pkcs11/Makefile \
          usr/lib/Makefile \
          usr/lib/pkcs11/Makefile \
          usr/lib/pkcs11/api/Makefile \
          usr/lib/pkcs11/api/shrd_mem.c \
          usr/lib/pkcs11/ica_stdll/Makefile \
          usr/lib/pkcs11/ica_stdll/tok_struct.h \
          usr/lib/pkcs11/ica_s390_stdll/Makefile \
          usr/lib/pkcs11/ica_s390_stdll/tok_struct.h \
          usr/sbin/Makefile \
          usr/sbin/pkcsslotd/Makefile \
          usr/sbin/pkcs11_startup/Makefile \
          usr/sbin/pkcs11_startup/pkcs11_startup \
          usr/sbin/pkcs_slot/Makefile \
          usr/sbin/pkcs_slot/pkcs_slot \
          usr/sbin/pkcsconf/Makefile \
          usr/sbin/pkcscca_migrate/Makefile \
          usr/lib/pkcs11/methods/Makefile \
          usr/lib/pkcs11/leeds_stdll/Makefile \
          usr/lib/pkcs11/soft_stdll/Makefile \
          usr/lib/pkcs11/soft_stdll/tok_struct.h \
          usr/lib/pkcs11/bcom_stdll/Makefile \
          usr/lib/pkcs11/bcom_stdll/tok_struct.h \
          usr/lib/pkcs11/cr_stdll/Makefile \
          usr/lib/pkcs11/cr_stdll/tok_struct.h \
          usr/lib/pkcs11/aep_stdll/Makefile \
          usr/lib/pkcs11/aep_stdll/tok_struct.h \
          usr/lib/pkcs11/tpm_stdll/Makefile \
          usr/lib/pkcs11/tpm_stdll/tok_struct.h \
          usr/lib/pkcs11/cca_stdll/Makefile \
          usr/lib/pkcs11/cca_stdll/tok_struct.h \
          usr/lib/pkcs11/methods/4758_status/Makefile \
          misc/Makefile \
          misc/pkcsslotd \
	  testcases/Makefile \
	  testcases/common/Makefile \
	  testcases/driver/Makefile \
	  testcases/init_tok/Makefile \
	  testcases/mkobj/Makefile \
	  testcases/oc-digest/Makefile \
	  testcases/rsa_keygen/Makefile \
	  testcases/rsa_test/Makefile \
	  testcases/speed/Makefile \
	  testcases/test_crypto/Makefile \
	  testcases/threadmkobj/Makefile \
	  testcases/tok_obj/Makefile \
	  testcases/v2.11/Makefile \
	  testcases/login/Makefile \
	  man/Makefile \
	  man/man1/Makefile \
	  man/man1/pkcsconf.1 \
	  man/man1/pkcs11_startup.1 \
	  man/man5/Makefile \
	  man/man5/pk_config_data.5 \
	  man/man7/Makefile \
	  man/man7/opencryptoki.7 \
	  man/man8/Makefile \
	  man/man8/pkcsslotd.8])

echo
echo "CLFAGS=$CFLAGS"
echo

